/*
 * Created by songfei on 3/4/19 9:59 AM
 * Copyright (c) 2019 Tencent
 *
 * Description:
 */

import 'package:assets_audio_player/assets_audio_player.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app_1_25_0/testWidget/custom_input_cell.dart';
import 'package:logging/logging.dart';

Logger _logger = Logger('app_observer');

class AssetAudioPlayerDemoPage extends StatefulWidget {
  const AssetAudioPlayerDemoPage({
    @required this.title,
    Key key,
  }) : super(key: key);
  final String title;

  @override
  State<StatefulWidget> createState() {
    return _AssetAudioPlayerDemoWidgetState();
  }
}

class _AssetAudioPlayerDemoWidgetState extends State<AssetAudioPlayerDemoPage> with WidgetsBindingObserver {
  final AssetsAudioPlayer _player = AssetsAudioPlayer.newPlayer();
  final Playlist playlist = Playlist(audios: [
    Audio.network(
      'https://edu-data-1251316161.cos.ap-guangzhou.myqcloud.com/mf_resource_20210304/upload/shuoba_spoken/exam/6de/14/2/8d86e3d694df667/audio.mp3',
      metas: Metas(title: 'hello world'),
    ),
  ]);

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    _player.open(
      playlist,
      autoStart: false,
      showNotification: false,
    );
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Container(
          // color: Colors.green,
          width: flexible(context, 860.0),
          child: Column(
            children: [
              GestureDetector(
                onTap: () {
                  _player.play();
                },
                child: Container(
                  // color: Colors.redAccent,
                  width: flexible(context, 100.0),
                  height: flexible(context, 50),
                  child: Text('播放'),
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: flexible(context, 10.0)),
                child: GestureDetector(
                  onTap: () {
                    _player.pause();
                  },
                  child: Container(
                    // color: Colors.redAccent,
                    width: flexible(context, 100.0),
                    height: flexible(context, 50),
                    child: Text('暂停'),
                  ),
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: flexible(context, 10.0)),
                child: GestureDetector(
                  onTap: () {
                    _player.play();
                  },
                  child: Container(
                    // color: Colors.redAccent,
                    width: flexible(context, 100.0),
                    height: flexible(context, 50),
                    child: Text('继续'),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.paused:
        _logger.info('app paused.....');
        break;
      case AppLifecycleState.resumed:
        // SyncedDateTime().syncTime(); //切换前后台时重新对时
        _logger.info('app resumed.....');
        break;
      case AppLifecycleState.inactive:
        _logger.info('app inactive.....');
        _player.pause();
        break;
      default:
    }
  }
}
